package com.meida.module.bus.provider.handler;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.meida.common.base.module.ExportField;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.ExportInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusReportOutfour;
import com.meida.module.bus.provider.service.BusReportOutfourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component("busReportOutfourExportHandler")
public class BusMaterialfourExceporttHandler implements ExportInterceptor<BusReportOutfour> {
    @Autowired
    private BusReportOutfourService busReportOutfourService;
    @Override
    public void initExcelExportEntity(ExportField exportField, List<ExcelExportEntity> entityList) {
        String key = (String) exportField.getKey();
        String name = exportField.getName();
        if (FlymeUtils.isEmpty(key)) {
            List<ExportField> children = exportField.getChildren();
            List<ExcelExportEntity> excelExportEntityList = new ArrayList<>();
            for (ExportField childrenExportField : children) {
                childrenExportField.setGroupName(name);
                if ("本期申报量".equals(name)) {
                    exportField.setKey("a");
                }
                if ("本期审核量".equals(name)) {
                    exportField.setKey("b");
                }
                if ("前期累计已完工".equals(name)) {
                    exportField.setKey("c");
                }
                if ("截止目前累计完工进度".equals(name)) {
                    exportField.setKey("d");
                }
                excelExportEntityList.add(new ExcelExportEntity(childrenExportField.getName(), childrenExportField.getKey()));
            }
            exportField.setList(excelExportEntityList);
            exportField.setChildren(new ArrayList<>());
        }
    }

    @Override
    public ResultBody initData(Map params) {
        ResultBody body=new ResultBody();
        List<EntityMap> busReportOutthreeList = busReportOutfourService.getExportData(params);
        List<EntityMap> list=new ArrayList();
        if(FlymeUtils.isEmpty(busReportOutthreeList)){
            list.add(new EntityMap());
            body.data(list);
        }else{
            body.data(busReportOutthreeList);
        }
        for (EntityMap entityMap : busReportOutthreeList) {
            List<EntityMap> list1 = new ArrayList<>();
            EntityMap entityMap1 = new EntityMap();
            entityMap1.put("beforefinishPercent", entityMap.getBigDecimal("beforefinishPercent"));
            entityMap1.put("beforefinishCount", entityMap.getBigDecimal("beforefinishCount"));
            entityMap1.put("beforefinishPrice", entityMap.getBigDecimal("beforefinishPrice"));
            list1.add(entityMap1);
            entityMap.put("a", list1);

            List<EntityMap> list2 = new ArrayList<>();
            EntityMap entityMap2 = new EntityMap();
            entityMap2.put("currentAuitPercent", entityMap.getBigDecimal("currentAuitPercent"));
            entityMap2.put("currentAuditCount", entityMap.getBigDecimal("currentAuditCount"));
            entityMap2.put("currentAuitPrice", entityMap.getBigDecimal("currentAuitPrice"));
            list2.add(entityMap2);
            entityMap.put("b", list2);

            List<EntityMap> list3 = new ArrayList<>();
            EntityMap entityMap3 = new EntityMap();
            entityMap3.put("declardPercent", entityMap.getBigDecimal("declardPercent"));
            entityMap3.put("declaredCount", entityMap.getBigDecimal("declaredCount"));
            entityMap3.put("declaredPrice", entityMap.getBigDecimal("declaredPrice"));
            list3.add(entityMap3);
            entityMap.put("c", list3);

            List<EntityMap> list4 = new ArrayList<>();
            EntityMap entityMap4 = new EntityMap();
            entityMap4.put("newfinishPersent", entityMap.getBigDecimal("newfinishPersent"));
            entityMap4.put("newfinishCount", entityMap.getBigDecimal("newfinishCount"));
            entityMap4.put("nefinishPrice", entityMap.getBigDecimal("nefinishPrice"));
            list4.add(entityMap4);
            entityMap.put("d", list4);
        }
        return body;
    }


}
